<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    append-to-body
    :close-on-click-modal="false"
    @close="closeDialog()"
    v-model="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter="dataFormSubmit()" label-width="150px">
    <el-form-item label="主键id" prop="id" v-if="dataForm.id">
        <el-input v-model="dataForm.id" placeholder="主键id" disabled></el-input>
    </el-form-item>
    <el-form-item label="用户id" prop="userId">
        <el-input v-model="dataForm.userId" placeholder="用户id" disabled></el-input>
    </el-form-item>
    <el-form-item label="乡镇、街道" prop="level2Code">
        <el-input v-model="dataForm.level2Code" placeholder="乡镇、街道"></el-input>
    </el-form-item>
    <el-form-item label="村、社区" prop="level3Code">
        <el-input v-model="dataForm.level3Code" placeholder="村、社区"></el-input>
    </el-form-item>
    <el-form-item label="组别" prop="houseGroup">
        <el-input v-model="dataForm.houseGroup" placeholder="组别"></el-input>
    </el-form-item>
    <el-form-item label="门牌号" prop="houseNumber">
        <el-input v-model="dataForm.houseNumber" placeholder="门牌号"></el-input>
    </el-form-item>
    <el-form-item label="户主" prop="houseHolder">
        <el-input v-model="dataForm.houseHolder" placeholder="户主"></el-input>
    </el-form-item>
    <el-form-item label="是否常住" prop="permanent">
      <el-radio-group v-model="dataForm.permanent">
        <el-radio :label="1">是</el-radio>
        <el-radio :label="0">否</el-radio>
      </el-radio-group>
    </el-form-item>
    <el-form-item label="联系电话" prop="phone">
        <el-input v-model="dataForm.phone" placeholder="联系电话"></el-input>
    </el-form-item>
    <el-form-item label="运营商" prop="op">
        <el-input v-model="dataForm.op" placeholder="运营商"></el-input>
    </el-form-item>
    <el-form-item label="家庭通信消费支出" prop="houseCConsume">
        <el-input v-model="dataForm.houseCConsume" placeholder="家庭通信消费支出（单位：元/月）"></el-input>
    </el-form-item>
    <el-form-item label="当前套餐" prop="currentSet">
        <el-input v-model="dataForm.currentSet" placeholder="当前套餐"></el-input>
    </el-form-item>
    <el-form-item label="当前套餐到期时间" prop="menuExpireTime">
        <el-input v-model="dataForm.menuExpireTime" placeholder="当前套餐到期时间"></el-input>
    </el-form-item>
    <el-form-item label="走访情况" prop="visitDetails">
        <el-input v-model="dataForm.visitDetails" placeholder="走访情况"></el-input>
    </el-form-item>
    <el-form-item label="现场照片" prop="siteImage">
        <el-input v-model="dataForm.siteImage" placeholder="现场照片"></el-input>
    </el-form-item>
    <el-form-item label="详细地址" prop="address">
        <el-input v-model="dataForm.address" placeholder="详细地址"></el-input>
    </el-form-item>
    <el-form-item label="备注" prop="remark">
        <el-input v-model="dataForm.remark" placeholder="备注"></el-input>
    </el-form-item>
    <el-form-item label="经度" prop="longitude">
        <el-input v-model="dataForm.longitude" placeholder="经度"></el-input>
    </el-form-item>
    <el-form-item label="纬度" prop="latitude">
        <el-input v-model="dataForm.latitude" placeholder="纬度"></el-input>
    </el-form-item>
    <el-form-item label="创建人" prop="createBy" v-if="dataForm.id">
        <el-input v-model="dataForm.createBy" placeholder="创建人" disabled></el-input>
    </el-form-item>
    <el-form-item label="更新人" prop="updateBy" v-if="dataForm.id">
        <el-input v-model="dataForm.updateBy" placeholder="更新人" disabled></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="createTime" v-if="dataForm.id">
        <el-input v-model="dataForm.createTime" placeholder="创建时间" disabled></el-input>
    </el-form-item>
    <el-form-item label="更新时间" prop="updateTime" v-if="dataForm.id">
        <el-input v-model="dataForm.updateTime" placeholder="更新时间" disabled></el-input>
    </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="visible = false">取消</el-button>
        <el-button type="primary" @click="dataFormSubmit()" v-if="canSubmit">确定</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script>
    import {getObj, addObj, putObj} from '@/api/smart/setrecord'

    export default {
    data () {
      return {
        visible: false,
        canSubmit: false,
        dataForm: {
          id: '',
          userId: '',
          level2Code: '',
          level3Code: '',
          houseGroup: '',
          houseNumber: '',
          houseHolder: '',
          permanent: '',
          phone: '',
          op: '',
          houseCConsume: '',
          currentSet: '',
          menuExpireTime: '',
          visitDetails: '',
          siteImage: '',
          address: '',
          remark: '',
          longitude: '',
          latitude: '',
          createBy: '',
          updateBy: '',
          createTime: '',
          updateTime: '',
        },
        dataRule: {
          userId: [
            { required: true, message: '用户id不能为空', trigger: 'blur' }
          ],

          level2Code: [
            { required: true, message: '乡镇、街道编码不能为空', trigger: 'blur' }
          ],

          level3Code: [
            { required: true, message: '村、社区编码不能为空', trigger: 'blur' }
          ],

          houseGroup: [
            { required: true, message: '组别不能为空', trigger: 'blur' }
          ],

          houseNumber: [
            { required: true, message: '门牌号不能为空', trigger: 'blur' }
          ],

          houseHolder: [
            { required: true, message: '户主不能为空', trigger: 'blur' }
          ],

          permanent: [
            { required: true, message: '是否常住：1-是；0-否不能为空', trigger: 'blur' }
          ],

          phone: [
            { required: true, message: '联系电话不能为空', trigger: 'blur' }
          ],

          op: [
            { required: true, message: '运营商不能为空', trigger: 'blur' }
          ],

          houseCConsume: [
            { required: true, message: '家庭通信消费支出（单位：元/月）不能为空', trigger: 'blur' }
          ],

          currentSet: [
            { required: true, message: '当前套餐不能为空', trigger: 'blur' }
          ],

          menuExpireTime: [
            { required: true, message: '当前套餐到期时间不能为空', trigger: 'blur' }
          ],

          visitDetails: [
            { required: true, message: '走访情况不能为空', trigger: 'blur' }
          ],

          siteImage: [
            { required: true, message: '现场照片不能为空', trigger: 'blur' }
          ],

          address: [
            { required: true, message: '详细地址不能为空', trigger: 'blur' }
          ],

          remark: [
            { required: true, message: '备注不能为空', trigger: 'blur' }
          ],

          longitude: [
            { required: true, message: '经度不能为空', trigger: 'blur' }
          ],

          latitude: [
            { required: true, message: '纬度不能为空', trigger: 'blur' }
          ],

        }
      }
    },
    methods: {
      init (id) {
        this.visible = true;
        this.canSubmit = true;
        this.$nextTick(() => {
            // this.$refs['dataForm'].resetFields()
            this.dataForm.id = ''
            this.dataForm.createBy = ''
            this.dataForm.updateBy = ''
            this.dataForm.createTime = ''
            this.dataForm.updateTime = ''
            if (id) {
            getObj(id).then(response => {
                this.dataForm = response.data.data
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.canSubmit = false;
            if (this.dataForm.id) {
                putObj(this.dataForm).then(data => {
                    this.$notify.success('修改成功')
                    this.visible = false
                    this.$emit('refreshDataList')
                }).catch(() => {
                    this.canSubmit = true;
                });
            } else {
                addObj(this.dataForm).then(data => {
                    this.$notify.success('添加成功')
                    this.visible = false
                    this.$emit('refreshDataList')
                }).catch(() => {
                    this.canSubmit = true;
                });
            }
          }
        })
      },
      //重置表单
      closeDialog() {
          this.$refs["dataForm"].resetFields()
      }
    }
  }
</script>
